package com.blb.thread;

public class Demo5StopThread {
    //控制线程停止的标志位
    private static boolean flag=false;

    public static void main(String[] args) {
        //启动子线程
        new Thread(()->{
           for(int i=0;i<100;i++){
               if(flag){
                   System.out.println("线程停止");
                   break;
               }
               System.out.println(Thread.currentThread().getName()+"---->"+i);
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        }).start();
        //主线程5s后停止子线程
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        flag=true;
    }
}
